package mylibrary.xml.demo.suit;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class EncryptedData {

	Element encData = null;
	Document doc = null;
	
	private boolean elementAppendedToDoc = false;
	
	public EncryptedData(Document document) {
		doc = document;
		encData = doc.createElement("EncryptedData");
		encData.setAttribute("xmlns", "http://www.w3.org/2001/04/xmlenc#");
	}
	
	public void setId(String id) {
		encData.setAttribute("Id", id);
	}
	
	public void setType(int type) {
		encData.setAttribute("Type", AlgoNames.getAlgoNSValue(type));
	}
	
	public void setArbitraryType(String mimeType) {
		String typeValue = "http://www.isi.edu/in-notes/iana/assignments/media-types/" + mimeType;
		encData.setAttribute("Type", typeValue);
	}
	
	public void addChild(Document document) {
		NodeList nList = document.getChildNodes();
		for(int i=0; i<nList.getLength(); i++) {
			Node tempNode = nList.item(i);
			if(tempNode.getNodeType() == Node.ELEMENT_NODE) {
				Node importedNode = doc.importNode(tempNode, true);
				encData.appendChild(importedNode);
				return;
			}
		}
	}
	
	public Document getEncData() {
		if(elementAppendedToDoc == false) {
			doc.appendChild(encData);
			elementAppendedToDoc = true;
		}
		return doc;
	}
}
